"""
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
"""
__author__ = 'Danyang'


class Solution:
    def maximalRectangle(self, matrix):
        """
        Algorithm 1:
        brutal force, iterate from top-left points and expand the area.

        Algorithm 2:
        Similar to 082 Largest Rectangle in Histogram with additional height calculation
        Scanning row by row. (possible to use another dp)
        
        Algorithm 3:
        Without using stack, keep two dp arrays
        https://github.com/algorhythms/LeetCode-Java/blob/master/src/MaximalRectangle/Solution.java

        reference: http://shanjiaxin.blogspot.sg/2014/05/maximal-rectangle-leetcode.html
        :param matrix: a list of lists of char
        :return: int
        """
        if not matrix or not matrix[0]:
            return 0

        # matrix = map(lambda x: list(x), matrix)  # not necessary

        global_max = -1<<32
        m = len(matrix)
        n = len(matrix[0])

        # added dp to get heights
        dp_height = [[-1 for _ in xrange(n)] for _ in xrange(m)]
        dp_height[0] = map(lambda x: int(x), matrix[0])
        for i in xrange(1, m):
            for j in xrange(n):
                if matrix[i][j] == "1":
                    dp_height[i][j] = 1+dp_height[i-1][j]
                else:
                    dp_height[i][j] = 0

        # end of dp

        for i in xrange(m):
            # Largest Rectangle in Histogram
            inc_stack = []

            j = 0
            while j<=n:
                # if not inc_stack or j+1<n and dp_height[i][j]<=dp_height[i][j+1]:  # mistake
                if not inc_stack or j<n and dp_height[i][j]>=dp_height[i][inc_stack[-1]]:
                    inc_stack.append(j)
                    j += 1
                else:
                    last = inc_stack.pop()
                    # height = self.get_height(matrix, i, last)
                    height = dp_height[i][last]
                    if inc_stack:
                        global_max = max(global_max, height*(j-(inc_stack[-1]+1)))
                    else:
                        global_max = max(global_max, height*j)

        return global_max

    def get_height(self, matrix, row, col):
        # possible replace by dp; otherwise TLE
        height = 0
        for i in xrange(row, -1, -1):
            if matrix[row][col] == "0":
                break
            height += 1
        return height


if __name__=="__main__":
    assert Solution().maximalRectangle(["01", "01"])==2
    assert Solution().maximalRectangle(["10001000001111110111111010111100111101001111001011111111101110110110011101101001110011101111001010110100110110111110101111011111110101111111111111111111111101110111111111000111100111111111011010111011","11010111100111111111111011110111111011101111111010101100110001100110101111101101111111010011111011111110111001110111010111111111011111110110011010111110101110111010101101101101110111111101111110100110","11010001100111011011111111111110111111010011011011111011111111110111111110111101111011111101101100111111001111101010111111011101110111011111001111111111111111101111110111111111111010101011111011111111","11111111011111110010110111001111111111111101111111111110100101111101110110001111100110101101111100111111110111001010110100011111111001010011100101011011011111111111110111111101001101110001110111110111","11011000010101101111011101011101101100111111101111111111111001010011011110110111110111111011100000001111111100011011101110110000000110001111111111111110111011011100000111111111110001111100111111111111","11111001101111101111111100111101111111111001011101111111111011111011110111111011110101111111111111111111010111111111111011111110110101111111111001110011010101111011111111111111011011100110111111101111","00111111011011111111100111110111011111111101101011111011110111100111111111111111111001111110000111111011011010111101110111111101111010111011011111110111101101110011111011111111110101111111011110011111","00101110111111011111110110001110111101111111111001111010010101101111010011101111111011011110110111001011001111100101110111111101000111101110111101111111100110011100111011111111011111110111111111110111","11111111011011011101111110111011110111011111111001101111111111011010111101110111111111111111111001000110111111001011011111010110111111101101100111110011110111111111011101110101111110101111111101101111","11101100010111111101111111101110110110111111101111111101111111110111001111000111110110111101111111011110111101101111101111101111010111011111011111101011101111110111111111111111110011111110111110111001","11110111111111001111111101011011101101111111100111111011111101011111111010110011111111111111111011010111111110111101110101111111011111111010011101111101011111110111100001110100111111110111011000011010","11111111101011110010101111101000111111011111101001010111111111110111010110101111101111111111111100101111111111011001101110101011001111111110011111111001111011111111011100111011111111111001110111010111","11111100101110111011110111111101111011111100111011011011111011111011111101000101101101111011111011101111011010100110011110011110011101101101110111111101101111110111110111110101111111100111111111111011","10111111011011000111111101111110001111111110111111111111111111111100111111110111011101111101101011011110010111111111111111101111101011111110111011011011111111111010001110111111110001111111111111111011","01001110100111011111001011011111111111100111110101111111111011110101111111011010110101111111110101010110011011111011100011111011111001101101011111101111011110111011101001101111111101100111111111111111","10111011111011011110111010111101111111011111111110111010011110111011111111111111111111111011111110111011110111111110110111110111011111101111111101011010001100100011101011101111111111111100110111110111","10111000101111111111111111110011010100110010101111101001111111110111111101111101100001110111111111111011110111111011111111110111111111111110111011100111110111111101111111111111111011111111011111111111","10101111111100111011111110111100111010111111110111010110111111111010001110101110111110101111111110111101011111110011000111111111111111111011101101111011011111111011111111101110111101101111111111101111","11111111111111001111111011011111111011000111111100000111101110101110011011111111110110111110101111110011111101110111111101111111110010111011111111101111110111111010011110111011111111111111111111100111","11100101111111111111110111111110111111111101101010111111011101011011111111101001111011111001100111111101011111111101111111011110111100111011111101011101111111111001010101010111111111111101111111111111","11100001111010011111110011001111011011110011110111101110101111111111011011110101111101101011011010111111111111111111111000111111110111011100111100111111101101101011111101110111111011100110110101011110","11011110110111110011011101001111111111101111000101111011111011010111111011111011111011111011111111111011110010111111111111111101111111100111111111101111111111111110111101111101011001110001010111110110","11111110110111111111110101101001111111111101110111100110110110101111110100111011101111111101111001111111110111111001111011111111101101111101111111100001110011111011011011111111111010001101110111111011","10111110111111110111111110111111111110111111011010111000011101011111111101101101111111101111111111101011111111110111111110101111111111111101111011110111111011111111110110111111011011111110110110110111","10111111001111111011110011011110101111111111111011111111111110001111111011101101111111101011001111100111111111111111111111111111101101010111111100011101011111111111010011111111011111111011111110111110","11101011111110111011111111111110110111111101111111111011111001111111111101011111111110011111011101010101111110001111111111101111011111101111110101010110010101111111101111111110111101101111000111111001","11111011101111101001000111011011111010101011111111111111011111110111111111011111111100001101101111111111111111111111101111111111100110111111111111011111111011101111111110111110111101111011101011110110","11111111101110110011011111110111010111111111110111111010111101101110101011111101110011001000110101011101111111010011111111111111011111111011110111111111011110111111010111011111101111110111011111111011","11010111110011110011111101011101111111011011111111111110011111110101101101111100110111111011010010010111001101111101111111011011011011110001111111110111110101101101101111111111011111101011111111111111","11001111100111111101011111000011111111111110111101010111011111101000111111101111110101111111011111111111111111101111111010101111110101110011111101010010011111100110001000111110001111101011111110010110","11111111111011011101011101001101011110111010111111111100000011111011110110110111111011101100111111111101110001110110011111111111111111110111111111111001111011111011111111001111011101111111110110011011","11011101111111111111111111101111111111100101111111111101111111110001101111110111110100101011111111111010111101101111111111011101111001111111111001110111110101110101111100111101110111111011111111111110","11111111111011111111101111110111111101010111111110000101011001110111111111111111111000111111100111111101101111111011011101111110111100110011101100101011100111111110111111011111111111001111101111111011","11111101111011110111111111001111100001111111010011111101100101111001110111111111111111110111110110110101111110111010010001101111101111111100110111111111000101111011011110111111111110111110111001101111","11001111011110111011011011000111110111110001111111110101101111111011110101110111111111111111111111000011111110111111111001111011101101111000101000111110111011101111111111111111110101011111101111111011","10111101001111011101111111011111110111101111110100111011111101111111101010111111111110111111100111111111110111101111110111111101111111000001111111110111010100111110010010111011111111111101111011011011","01111100001110111101111111110010111110110111011111111001111111011010011001111011111011010111011110111110011111111001101111111111111111110111111110100010011011111010101111111001111011110110011011111101","10110001111111101111111110101111001011111111111110011010110101111111111111011011011101101111101110101110110011111011101111111111111011001100111101100111101101010111111111111101011111011011111111001110","11111101110011111111011111111111111011111111011111111111111011111110001111111111111101111111110111111101101111011110111111111101011111111101011010110111111111111111011110100011111101011100111111111111","11111110111111110111011011111111111111111001111111111101111000111001011111101010110111111011010010111010111011001011010111111110011110100101111111111111111011111101101100111111000111011111010111110111","11111110111111101011001001101111111110101101011111111111111101111110100010111110101111111111110101111110111111000011111111001111111000010101111101111111011111110111110110100110111011001111111111111111","10111101111111100011100101111011110110001110100101111111001111111111111111101101011110110001111110111001110010111011011011101010011111111101010111010111101110111111101111111011101111111111110111111110","11101001011111111111001111111011111001101111011111011001110111011101011011111111111010111111111111111010100101011011111111111100101100011001101111111110111011100100100010101011110101110111111111111111","11111111111111010111111111011111010111010000101010110101110111111110111111110111110111111000111011011111101110110111110110111110111101110111111111111110111111011110111110000111110010110011111100111011","01111011111111101111111110110001011111011110101001111011011111111011110111111111101100101011101110111101111111011101111111001111111010111100111101010110110011111111111011110100101010011100111111111011","11111111111111111111110111011111110111101110111101001111110101111101101111111111111111111111011111001011111010001110011110111011111111111111011111111001110111111010111110111101011110101111110111110111","10011111110111101111111111111001001100101011111111111011101111101111111101111111010111001111011111011110101111011111111010111111011110111011110110101111100011101111110001111111111111111111111110010011","11100011111111111111010000111111010011111000101111111111101110111111100100111111110001110111011101111111101111011111111101110011101110001111111011111111111111011111110111110101111110111011110001111111","01100110001110010111111111111111110101111110111100111111110110101111110011111111110101111111111111111101110111011011101011110011101011101111111111011111000011111111111111111111111111111101111110111111","11111111111101110110011100111111111100101111011110110110111111111111101100111010110111101110101010110011111111111101011111101101111111101010110010111110101011111110011111111111101111101110011101101111","10101111111110101111111111110111111111011111110111110111011111011111110111111110011010111110001110111111111101011101111001111111111111111111011001111111011011111100110111111111111110111100111111111111","10101110011101111111101110001101111101111101110111011101101011110110111001110110011111001111111011100100111001001111111110111001111101000110001110101111101101111011111111111011011001111011111010111111","11111111111111101111010110011101111011111111111011010010011111001101010110011001100101100111011111011001101111101111111101000111110111011111101101111101101011101111110001111001100110010101011110111111","01111101101001111101111111111111111111110100111011010111011111111101111101111111011000111111111111110110111111110111111010111111111110111101101111101111111101011111101101111100111111011110111111101111","11001010011101111011111111111110011111110001101111110011110111111110111000010100111111111111011101111011011111111000111111111010101111011111110010001110111111011110111110001011111111110111011111010101","10111111100110111111101111110111111111011101000111101110101111101110111100100011101011101011111101100110110111111111101100111111110011011011010110110110110011011011110111011011001011110111111011111101","10110110111011111101111011111111111111110111110010101000111101110111111111101111111111100010111111111101111111111111111111101111101111011110011111111111101010110101101110011111111110001100101101011111","11010111010111111111110101111100101110111101011100011011011111111100011111110000111110111101010101101111111111011110011111011111111111111111011101011101101001101111111110101011110010111111011111111011","11111111111011111101111111101111100111010101111101011111111110110111011101111111111001110011011011110010110111111111110011110111100011001011101011111101110101101000110111111011111110110111011111111101","11101010111111101101111010110011111111111111011001100101101101110110111001111111011111011101110011011111111111111001101111111111101110111111111111111110101111011101111110011110111111101111101111011111","11000101100111111100101111001111111010111111101111111101111111110111111111001111111111100111010110111110111011010011111111110101111001111101011000111111011011110110110110011111111111111111100111101111","11111111111111111111111111110111111011001111110000110111110111111110111011101011011011111101011100110011111110111011100110111111011111110111101101111011111111111111101110110111001100010101011110011111","11111001011101101111111111001110111111011111111001011111111110101110111001111011111101111110100111111111000101111111110101111111111110010000111001101111111000111101111111111111011111101001011111101100","11110110111011011101111011011011011011011110011111111111111111011110111111111111111111101111111110111110011111111111111110011110111101001111011101100111111010101101111101100110111111111111101111110100","11111111111101111010011101001111011101100111011111101111111000101111111010011111111110110101110111011110111110111001110010011111111001011101011100101111111111110110111010101111111110111111001111111111","11111101111110111111111111011110111101011101111010111111111100111111110111001111111111101011011110111111111001111110110101001110111011101001111011011011111111110111011111110111011101111100111111111111","11111111011011111111111110101101011011111111011110111011111011101101111101001110110011100101111111111111111101111110010010101110111111011101110110111010010100111101001111110011011011010101111111101111","11011111101111111111111111111110111101111111001011111111001101111111111100110111110001101011110110111101100111011011101111100110100110010011111111011001111111111011011011111000111111111011111110111011","11111111101111110111111111001111111110101111110100111100100011101111111100111111101011001101000111100010001111101101011111110101111111011100010111111110111111110111110101110001110010011111111101011111","11111111111101111111011111101011011111011111110110111011111111111011111111101010011111101111001101011110011100101110110111111111011111111110111110111110011111011111110001101111111011110110100111111101","11011101011111101101101110011011100010111100101111111010101101111111100111111110011111111001100111111101011111110011111101111110011111111111110111101111110111111111100001101111111111011110011111011111","10111111011111000111101111101111111001001111111110111111100011111011011111001110010111111111011111110001110111111110111100101110101101001011111011111111111110111111011111001111011101011111111011111110","01111011111111110110011111110011111011001111111101101001111111111111111111010111111111101111111101010111011101111110111111111011010011111011111101101110111011111111010110010110101100111110111111111111","01111111111110111110111111001111111111101111111110101111110000111001111011111111111101111111001101000111111011111111110101000110110111010101111011111111101101111111011111110010111111101111101111110101","10000101110111101111111001001111111111100111111100111111011000111101111101111011011110111100111001110011111011111011101110111111101110101111111111111101011110110111011111110101111111011111100111111111","10111111001111111101110111110011111110010101111010111111110101111010011101111111111111000110100111111011101110111011111111011100111011101111100111100111111111101101101101110011111110101011101111001100","10110101111110111111110011111011011111111111111111011010101110001101111011000111110111111101111101111011011011111111010110101101010010100101111011110101110100001001111101101000111001111011011010111111","11111101111111111011101001111111111000110111010111101111101011110111011010101111111111111101111111111011111101010100111101111111101111010111011001011101110111111110111111110100101111111101111110011111","11001011011011111111001111111011111110110001110011101111111110111101111111111111101111101111111111111010111111011110110111111111111111111111110101110111111101011110111111111110111101100010101110111011","11110011111100000111010111011110000011011111111111011000101011111110011011111111111111111111010001010111110010111111111111110111101111001101010101111111111110110111011101101111111011100111111111111111","01111111111001011101000111111011100111111111111101101111110111111100101111101101011111111101011111101010101111111111101001001110000101110111000111111111101011011101011010111111111111101100111111111111","11011111111101011110011111110111111111010111011111111001110011111101111111111111111001001011100000011001111011111111010111011111111111111111100111111110001111111110111111101111111111111100111011111111","01100011111100001111001101111101011011110100110111011111111111011111100111101111111011111101111011101111001111111111111011111111001110111001111010011111011111000111111011111111101011111001111101110100","11111111111111101111111001001000111011111111001111111111101011011011011001101111111011111011111110111111101111010100111011111100111101111111111111101101011101101111111111111111011111111111001111101110","11111010111100111111101011011101111010101111111111111111001111111111111100111010101011111101111110111111110011001110111111111011110111111111011111101011111011101111101111110111101110110111111110110011","01000111100101101111110101001111110101111101111110110110111000111101111111101001111101111111111111111101111111111011111010101011111011111111111111101111100111011111011010101111111101111110011111111111","11111011111110111111101110101111111011010111111011101011111011101110111101010011011101011110011111110111011110110100101110110011111101111111111111111101011010111011111111111101110101110111101101001110","01111111011100101111101110111111111010010110111110110111111110110011111111100111111111101111111111111101111111110011111110111110001111110000111110011110101010011100111111110111110101111011011111111100","11101011110111011111111110111111111011110110111111011111101111111011101111110011111111100011111111101111000101011111111111011111111111011001101011110001111011111101101001111111010011111010111001111011","11111111111100011111101111111111101110110110011101111111111011011111101111110111101111110111111011111111001001111110011011111011111011111111001110101000111111101000110101101111111011011101100100111111","10110001111111111110111111101111111011111111111111101111111110100101011101011111011011110101111111111110111111111011111001101101111001001111111100011111101111101101111111011111111111110001111111011101","00111011111111110101011111011010111110101110111111001111010011111011000111111111011101110011110111111111110101111111010001111111001110011111011101111111100110110100001111011111101111111101101111111011","10011011011111100111111111001001001111010111011111011111111111111110011111111101001010101111111111111111101111111111110001111010111110110101111110101101101100111111110111111100111011111111011101011101","11111111011100010111111110110111011000101111110111101011111100111111111111111111101111101111111111000111111111111111010101011110010011111011011111110100100101101100111110111110001011011011110110011111","11111111111111110101001111101001111111111111111111011100110010100011101111110011111101111111101100010101111111111011111111101111111111111111111111111011111111111010101011001101111111110101011011111011","11110111010101101111111011111011110110111100110111111110110111110101111111111111011110111111111110111110101111110101010111110010011110101111011010111111111111111101111111110111111010101111111011001111","01101001111111100001110110100010111010101111011011010011111011001100111111100111011111111011111111101110101011111111111111011111011111111010111011011111011101100111011111011111111111010101111111010111","11111110101111100011111111110111110110101111111111110111101111111101110111111101110111110011110110101111111101111111101101111110110010101111111111111001111111111111110111111110011101010101111100111111","01011111011011011011110111111111011101101110010110111111101110001111111010111011010101001111111111101000001111111100100111111001111111111111011110010011111111111101110111111111101101111111001101110101","11110111100011111111101110111111101111111111111111101111001101111101111111101111110111111101110110101111111100111111100110111111110111101111111111111110111011011101111111101110101111111110011111011111","00110111111111100101111111111101111111111110111010111111011101111111011110111111111111110100001111111111111011111101110111101111100111011111111101110110111110110110111101111101011111110110111101111110","11111111110011010111111110111110111110111111111101011111110111011111100111000110110101010011111101001011111111001011111111101011111101111011111111111011101001111111111111111100111110111111011110111010","11110111111010111101111101010111111101110111101111101011001011101101111101111101111100100001111111011011111111111111111111011111011111001101111111111101101111110111111111110101111111011011110111111111","11111111111101011011110110111101111111111111110111001011100100111111111011101101001101001011111110110111011001111111111110100111111110011111110001111111101111011111110111111110101011111101111011111101","01001111111001111111101111101111111101111111111111101111110101111110011111110111011100111111010111111100011111111111111111111011110101101011111110110111111010111011011101111011111011110111110011111011","00011100100011111111111110101110111110111111110111111111011001110010001110011011110110111011111101101111001111110111110111111101111111111111111111111111111101111001110111101110101010001111111110111111","11011111100110111101111111111111111010000011111111011111101110111101111011111111011010111111110101101001111111111111011111000010110111111101111101001011011011111011010111011111011110111111111111001110","11101111111111101111010110111111010111011010101111111110111011011000101110110111111111110111110110110110111111011111010111111001010111100011111110011111011111111100111111011101110110111101111011010011","11011000110010100111001111111010111011010010000011111111111101111010111110100101111111001111111110111111111110001011110011111110101111110111111011000010111011011111011101111111111110110111110001111001","01110111011111011111101010110110111100111111101111111010011111111011111011101110111111111111100101111111111111111000011101001001111111110011111011011111011111001011010011111111111101111111111110111101","11110111110111001111111111111100101100111111111011111101010110110011010111111110101111111111111111111110011111101110011101111111100011011011111111101011000111011011111011001111010110111011111111001110","11111100111111010111111111111110111111110101110111011011111101110111011001111101101011111101111111111111111101111111010101111111110100011111110111011011111111110111111011111110010111111111110111001111","11111001101111111111011111111111111111101111011111110111101111111010010101101100011001111011101011111110110110101111100111111011111111100110101111110011111111111111010001011111110110011111110111101010","11111010111111111111111111100101010011010101111111101110011011010001101111110101011111111111110010110101110000111010111101001000001111011010011111111001001101111110111111111110001101101111101101110111","01011111111100110111010011110110101011111101110110111011110110100001111111111111110011110111001001111001111010111110110111111111111111110111111101110111111111111101011011100010111011110010111111011110","01011111111111111111011111111011111101111011111111001010101011111011110111011111111011010110111111111111101111110011011110111111110101111100011011010111111111111111111111111111111101010111110101111001","11101011100010010110011011111111111111111101101111111111011101110110111111111011000111110111110111111111101110111111110111001011101011101110111101011111111111010111111110111111011110011111111101001111","11111011101111101100011111001111111011111011111110111010111111110111011111111101111011111110011011111101010010011101101110111111111111111101101111111111111111111111111111011111011111111110110111111111","10111111101111110111011111111111111111101111111110111111111010111111100011011100011110011111111111111110110110011110111101110011101011110111110111111111111110100101011111111110011111110111111101011111","11110010111011111011101111110111101100111110111111111111001011111111011111110111111111111011111111111110101101111011110011011111111100110110111111011111110111111111101111111111110101111101111011111111","11111111101111111111010111101011111111011111010111111110110110001011111111111100011001011001000101101110111111110110111111111111111011011110101110111111110111111110111111111111111111111011110001111111","10111011111010110111011110101111101111111111111101111111101011011111101011111111110101111111111011111110110110111111111110111011111011011010100111111101111111011110110111111111100111011111011110111111","01010111111111111110111111111011111101101110111101110001110011011110110101110010111111001100111101111101110111110111000110101111110110011111110111111100111111110111111111000110111111011111111111101110","11010110101010111101110010001111111111101101111011110111111110111111101001111011101111111001011110111110011111111000011111011111011101101110111111011111110111001111011101011111111111111100001101101001","11011101110101101111111101111100111011110100111111011110111111110011111111111011111111010111110111010111111101111110111110111110110111011111110111111100100011011011111111110111110110110101101101111101","11011111111111111110111111110111110110011111110101101101101110001011011110011011111111111111011110110111111110111111111111111110111111111111111111101111111111110101011000101101101111010111111001101001","11011111111111111101111111110111111111001101101010101111111111100111111110101101110110111110110111111101110111111100100111001001110111111111011111001111011110111111101110111111111110111010011101110111","11110010111111011111011001101111100110111100101111111111010111011100111011011111110000011011111101111110111110111011110111010111111111111110111111111111111001111011010110110111111111110110111111011011","01110101111010110111011111111011110110111110010011110111110111111111111110001110101110001011111110110111111111111100110111101111111111101111011100011111111111011010110101011011100101111111111110111001","11111111110111110111001101111110111111010111000111111110111011111111001011111111111111100111111111111111110101111101011000101011101111010001001111101111111011111111010011011110110110011110101110111110","11111010111111011111111011101110111101110111110101110011101101111001111101110100011101011111111111101011111111011110011100110111010101110111111010011101111110111111111100111001101111111100011001110101","11111111111101011011111110001011110111101111110011101111111101011010010110111101011111101111111111101011001011101111011111101101101100011110010111111111111111101111111101111010111111111111001111110111","11101111110100011111101111111111101101111111101110111111110111000111101101011110101010100111111111101111011111111101111111110111111111110111011111111111111110111110010011110110101110101110110111110111","01111111011111111111111110111111110111110010111110111111101110011110101001011101101111111111110001011111011011111001111111100101100011010101110100111111001011111111110111011011110101011100111111100011","11111010111111010110001111111110011011001111111110010101111110000101110001111010011011010111110100111111111110111111111101011011110111011111111010111101001101110111110111111100111111011110110111011111","00111110011111101001111101111101111011011101011111111111111111111110111111110011111111111111011111001111110111111010111111110111111111101110111111001110101110111110111111010111101111001100111101011110","01110111110010111010110110111111101111111111111111111101111111110101111011011101110100111111111110111111111110011111110111011101111001010111101101101111111111111111010011111101111101110011010111111101","01111111111111111111010111111111110011111101111111111100011101111011110110111011001111111111111000111101111111011110100111111111111011111111111001111101111011111111111110111101111010111010111111111111","11110110110111111011100111011010101111101110101101111101011010111111001111101001111111111110111001101111011111111101011110111110011101101011111111111111111110111111111110011110111110101111010101111101","10011111111111001110101101011000101011111110111111111111111110111000111111110111011110111111101001110101111111110011110011111011111111101111000000111111101100111010111111111011100011111011011110011011","11010101110111111111101011111110111111111011111111101101011111111010111110101001111001101111110111101010011110111111111111111111111110110010011110111010111110111001101100111011001101100111100100000010","01111011011001000110011111011101111010111011111111011111111101011111111111111011111111001011110111111110111011111101111111110111111110111011110111011111111111000111011110111110111011111110010111111111","11001111101111101101010101111111101111101111010111111101111101111011111100101111000111100110111110001111111011111110011101110110111100011010111111101111111110111111011110101111111111101110100111111011","00110111010111111010011011011101111101110111010011011001101111111110111100111111111111101110001011011111111100111111101111111011100111111111101110110111111011111111111011111110011011111011111111111101","11100000011101110111110111110101111011111110111101011111110110001110111101110110010111011011011111111111000011111010110111100111111111010111111010101011101110111000011111111011110011111011110011011111","01111111111111111011111110111011101010111111011111111110111110100000011111010111110110111011011111111011110101111101111111111011111110011011111111101110011111010111110011111010000111111100001011110111","10100000011111111101111111011101111111011111111101111100111111111111110101000111101101110000111110111111011111100110111111111011001111011111111001111111111111111011011111111111011110111101011111110111","01101010101101111111111110110110010011010100111101111101111011011011010010101111110101101101101011110111111111011111101011111110011110111101101111111111111011011111110111110001110111011111101111111110","11111110110110011111101111101101111011000011011011001110000111110010111001111111110001011011100100110110011011111111111111111111111111111011101111010111010111011011101111111110001011010001110011011100","01111111111101101011011111100101111011110010110111111110111011110111111101111111111011100101110111111111101111101111111011110101111110011110011101101111101111101110111111111001111101010111101011011110","11111111111011111111111110111111101111011111001111110001011010110111011000011110101101111101110111010110111011011111101111111111111111111110111111111111111111111101111000101111101011110101110010111111","00110111111011111111011111011011010011110111101111111011110111011010111100111111101100111110111111101110111110100110110110111111111101111011110111111111111111111111011100111110101011001101111100100010","01111011111111111011110111111110111110111110011111111110101111111111110101111011111010010001010101101111101011001100111110111101011111110110111111111110011111111010011001111111111111111011110010111101","01101111111111111011111111110111111100110111111111011111111111111111110111111111111111100111101011111111011110110011111110111110111101101000011110110101110011011110110010011110011001111011101110111111","11011001111111111100101101011111111111111001110110010110101110001111011111111110111111110111111111110111111111010111101111111111111111101100100111111110111100111011111111101111110011110010011110111111","11111011110101101111111110111110010111100110110111110011011110011000111110111110001001101101100110001110111011011111111111110111101110101001100111110111011010111111111111001101111110111100111111111111","11101101111111111010101111111111101011111110110001110011110111001110111101111101111111011110110100111111111101011100110011011111111100011101100110101101001111111010011111011011011101000011110111111110","11111011111111111111101110010111000100111110111111011111111101001111111111011110011110101011011101111001111111111011101111110111011101011001011101100011111111111001101111001101111111111011111101111011","01110111000111101101110101111111010111111101110010101111111111011011111111111111101101001111111101110101011111111011111110111111101111110111110111011111101111011110111101111111111111101111101111001111","10111110011111110110000111111011111010011110011111011000111111111011011111111111111111010111110111101011111111101111101111111111001100111111001101111011110111111001011111111101011011111111010110111001","11111111011111101111110111011111101111111100101111110011110101111111111111011000111110011011111101101011011100111011011111111110101101001111011011111111001111111110111110101011101111111011010111011101","11111010011000111111110111111111110011100110111111011011101011111110010000011011111111101111110111111111111111111110100111011011010111111111110111111111011111110101111100111111111110011111111101110110","11011011111011111010101111111111111111110111111011111111110101111111111100110111101011111110011111111111100111111111111110111111101011101111001111111001111111101111110101101001010111110111011111000010","11111001110011111101111111111111100111111111011111111101111001111111111111111101110110111111011110001111111111011111110011010111101111110111011111011010011001111110111111111011111111110111101111101111","10111111111011111101111101111101110111011011111111110110010110011101101100010111010011011101101101111101111100111011101110110111110111111111111111111111110001011111110011101111111101100011111011111111","00111111110101101111100101101011111011110010101110101101111111111101011010010011111111111101011100100110111111111011111111011111111111100111111111111101110011101111111111111111011110111111111111111101","11111101011101101011011111010011111111011010111100001111111101111101010111111101101111011011111111011001111111111111001111011111011101011111111110110110011111011111111110111111111111111110111111111011","01011101111101011110110010111111110111111001101111101001011111111110001101111110111111111111111111111111111011111101110110111111101010101001111001110111111110100101111111110111011010100011111011111111","11111111111010110100110011101001111000101101111111111011101111111111011101111011111111011101111111111001111011110111110111011111111011101111011010101110011010111011111101111111111110111011011111010100","11111111100011111011110110111110110111101101111111110111111110101010111001110101111111011110100111111011110111011111110001101110111111111111111111110110101110111101101101111111100111110010010001111111","11111101101101011101011011110111111101011101111011110111111111110010111010110111001011111110000011111001011100011110111110110111110010111100100111111110101111001001010101101100011001111111011111011011","11110110101111111011010110110011100111101111111101010011010011111100010111101011110111110101110111111111111111111011111110101110111111011011111011111111111001101011010111111111110111110111101100100101","01111110111100001111010011110110011111111111111111011011111111101010001111111111011111101111001111111111110001101111110111111111111110111111011111010111111111011111101011111111111101011101111011110101","11101101111111110101011101111101111011010111011001111110011111101111010001101101011100101111111011110110111011011111100001111111001111101110011111101101101011111001111001101111111111011011110110011111","01111111011101111111111011111101111011011111111111110111010011101111111101101010111111110111101110111111111011001111000110011111111110111111111110110011011011110001110101111100101001110011111111010110","01101001011011111110011101000101101011111101011100101111110111010110110100011111111110101101101111101110111110110101111111011101111111111101110110111111111111111111111111111101111101010111000111010011","10111101111111100011011111111111111011111011111111011111111111011111111111000111101110111010111111011111111101011101101101101111111111110100101111101110100111111111110110100111101111111111011001101101","11111010010011111111101001111000111110111110001111101101101101111010111111111111100010101011110111101011111111111111111111100011011111001111010111011101101111101111101110111111101111111111001111111110","11110111111111111101111001111011101110110111101111111110111111011111111000100011111101111001101110011110111111101001111111111111100011011110110001111101101111011010111111100101111111001111111110111101","11111111100011001111100111000111111011111111111110000011111100111010111010111111111111111111110111101111101111111100111110111111001111011010111111101011011100100111011110111110111110111111101111111110","01011111011111111101110111011111010111111111101111101111111011111110111100111010111111011111111111111011100101101101111001111011111011111111111011011111110100011111001111011111111100110111111111111111","01111000110111011110111111111111110110111100101111111011111110011011111111010111111011000010011111101110011010111111110111110011111111111110111111111111110011101111110111111101111111111111111111111111","10110111001111010111010011011111011111111101111110011111110011111011101111111111001101010001111111111111110111111001111101011111111011111101110111101111111100101011101111111101111111011111101101111101","11111110001111111110101111101111100011011111110111111111111111011011010101110101111011011111001111111101110110111111110111110101111111111001011111111011010110011110111111011111011110111111111110111111","01111110011110111011110111011111111111111101110111111110110111001110111011111111111111101111111011011111111101111110000111111111100111101111110011011001111110111011111001111000101001111111111111111111","11111111111110111011111111111110101011111110111110101001111111111011111111100011110011111110011110111111111101110111111110111101111111011111101111111101011111011110111110111111101111111110111111111101","11111011011010111111111111101111101001100001011100111011110111011111110100011110111111110111110111111110111110111110001100111110011111101111001111110100111101100111111010110101111001111001111100111111","11011111110101111111110100111111111111111011110111111111111110111010011111110111110000111011111101101111001111101100111111101111111101101010110111111111111011111001111110110101011100011111101101011010","01100101111111101100011001111011111111110111111001100100111011101111111001111001111101111010101111110101110111110101011111101111100110111111111111111011111101111111111111111111110110011101111111111111","10111111110111101110111111100111101011110111110111100111111111101100011101111111011011111111101011001111111111011111011101011111111111111110111111110111011111111011011111110110111111111111011011011100","11111111111111111111111001110111110101110101100011111111110111101011110100110111001111101111110111000000111110010011100111101010111101111111111111111011110111111101110111101111010111111111111111101111","10101110111101011111111110111110011111111010011101111111011110111111101101011111100111111101010111111101100011101111111011111111110110110111011111111111111111101111110111111111111101110111011111100011","10111110111111101111011111110101111101011110000101100111111101111011111110111111101111110101110011111000101101110011110100111011001111111011000001110011011110110110111011111111101011111111001111111010","10111110110101001101011111111011011111100100000101111111111110111111110000110111111011111110001111100101111110101110011110011111111101111101011111111111110011111101111111110111011111001011011111111111","01100110111001111111111011100100110100111111001111110111100110111100111111111100111111101110100110011110111101111111111110000011111111100110111111111110110101101111000001001111011111110111010111011101","11101011111011110010110110101010111010001111110111011110110111111111111001111111011100111101100011111111111101101111111111000001111111011111011111101110110110110111110111111101111100111111111111111111","11110100011110111101011111011110111001100110111111111111111110011111110011011101111111111010011011011001111101010111111111111111111011110011111111110111011011111111011111101110011011110011110111111110","11110111111111110110111111000011111101110101111100110011111111101111011011101111001010111010111111111111110011111111111111111101110101110111111001111100111111001011111011111100111011011101111111001111","01111011111111111011011101111111111111001111101001010111111111011101101010110011111110101010001111100101011111111101111011111010110110110111111111111110010110111111100111111000111011010010011111111101","01111111111011110110011110101111110001110111111111111001111111010111011001110111111111111011110011111101011001111110111110011111010101101111101111111110111000001011110111101111111111111111011111111111"])==48
